package com.bjsxt.reveive;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 雷哥
 * @Date: 2020/10/11/17:15
 * @Description:
 */
@Component
@RabbitListener(queuesToDeclare = @Queue("new.queue"))
public class ReceiveMessage {

  @RabbitHandler
  public void receiveMessage(String content, Message message, Channel channel){
    System.out.println("来到了新的队列。。正在消费中");
    String messageId = message.getMessageProperties().getMessageId();
    long deliveryTag = message.getMessageProperties().getDeliveryTag();
    try {
      channel.basicAck(deliveryTag,false);
      System.out.println("签收成功:"+content);
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd Hh:mm:ss");
      System.out.println("签收成功的时间:"+sdf.format(new Date()));
    }catch (Exception e){
      System.out.println(e);
      System.out.println("消息消费失败");
    }
  }
}
